<?php

set_include_path(get_include_path() . PATH_SEPARATOR . '..');

error_reporting(~E_DEPRECATED & ~E_STRICT);
require_once 'simpletest/autorun.php';
error_reporting(E_ALL);

require_once 'jsppp.php';

class JsPPPProcessorTests extends UnitTestCase
{
    const File4CompiledText = "txt_file3\ntxt_file1\r\n\ntxt_file4\n";

    public function testIncludeOnlyOnce()
    {
        $processor = new JsPPPProcessor(__DIR__ . '/test_dir/');

        $result = $processor->CompileFile("file4.js");

        $this->assertEqual(
            self::File4CompiledText,
            $result
        );
    }    

    public function testIncludedFileList()
    {
        $processor = new JsPPPProcessor(__DIR__ . '/test_dir/');

        $files = array();

        $processor->CompileFile("file4.js", $files);

        $this->assertEqual(3, count($files));
        $this->assertTrue(in_array('file4.js', $files));
        $this->assertTrue(in_array('file1.js', $files));
        $this->assertTrue(in_array('sub_dir/file3.js', $files));
    }

    public function testCreateCacheFile()
    {
        $cacheDir = __DIR__ . '/test_dir/cache_dir';

        if (file_exists($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php'))
            unlink($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php');
        if (file_exists($cacheDir . 'file4.js'))
            unlink($cacheDir . 'file4.js');

        $processor = new JsPPPProcessor(__DIR__ . '/test_dir/');
        $processor = new JsPPPProcessorCacheDecorator($processor, __DIR__ . '/test_dir/', $cacheDir, time());
        
        $result = $processor->CompileFile("file4.js");

        $this->assertEqual(self::File4CompiledText, $result);
        $this->assertTrue(file_exists($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php'));
    }

    public function testReturnCachedContent()
    {
        $cacheDir = __DIR__ . '/test_dir/cache_dir';

        if (file_exists($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php'))
            unlink($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php');
        if (file_exists($cacheDir . 'file4.js'))
            unlink($cacheDir . 'file4.js');

        $processor = new JsPPPProcessor(__DIR__ . '/test_dir/');
        $processor = new JsPPPProcessorCacheDecorator($processor, __DIR__ . '/test_dir/', $cacheDir, time());

        $processor->CompileFile("file4.js");

        // change content of cached file
        file_put_contents($cacheDir . '/file4.js', 'cached content');

        $this->assertEqual('cached content', $processor->CompileFile("file4.js"));
    }

    public function testRecompileIfChangeOneOfIncludedFiles()
    {
        $cacheDir = __DIR__ . '/test_dir/cache_dir';

        if (file_exists($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php'))
            unlink($cacheDir . '/f0cfc42aaf242c22e78f80ea920cc7bf.php');
        if (file_exists($cacheDir . 'file4.js'))
            unlink($cacheDir . 'file4.js');

        $processor = new JsPPPProcessor(__DIR__ . '/test_dir/');
        $processor = new JsPPPProcessorCacheDecorator($processor, __DIR__ . '/test_dir/', $cacheDir, time());

        $processor->CompileFile("file4.js");

        // change content of cached file
        file_put_contents($cacheDir . '/file4.js', 'cached content');

        touch($cacheDir . '/file4.js', time() + 1);
        touch(__DIR__ . '/test_dir/file1.js', time() + 2);

        $this->assertEqual(self::File4CompiledText, $processor->CompileFile("file4.js"));
    }
}

?>